<?php
/**
 * 用户管理
 * @copyright (c) Seer All Rights Reserved
 */

require_once 'globals.php';

$Student_Model = new Student_Model();
$action = $_POST['action'];
$mode = $_POST['mode'];
$tel = isset($_REQUEST['tel']) ? $_REQUEST['tel'] : '';

//显示学生(学生一览表检索)
if ($action == '' ) {
	$page = $_GET['pid'] ? $_GET['pid'] : '1';
	$sqlSegment .= ' ORDER BY buildtime DESC ';
	$pwd = '学生管理';
	$addUrl = $tel ? "tel={$tel}&" : '';
	$usersNum = $Student_Model->getStudentsNum($tel);
	$students = $Student_Model->getStudentsForAdmin($sqlSegment, $page, $tel);
	$pageurl =  pagination($usersNum, Option::get('admin_perpage_num'), $page, "student_list.php?{$addUrl}pid=");
	include View::getView('header');
	require_once View::getView('user/student_list');
	include View::getView('footer');View::output();
}

// 学生删除
if ($action == 'delete' ) {
	$students = isset($_POST['ids']) ? $_POST['ids'] : array();
	foreach ($students as $val)
	{
		$Student_Model->deleteStudent($val);
	}
	seerDirect("./student_list.php?active_del=1");
}

// 学生信息编辑
if ($action == 'update') {;
	$ID = $_POST['ID'];

	// 编辑画面初期化
	if(!$mode)
	{
		$student=$Student_Model->getOneStudentForUpdate($ID);
		include View::getView('header');
		require_once View::getView('user/student_update');
		include View::getView('footer');
		View::output();
	}
	// 编辑信息提交
	else if($mode=="commit")
	{
		$ID = $_POST['ID'];
		$name = $_POST['name'];
		$tel = $_POST['tel'];
		$sex = $_POST['sex'];
		$birthday = $_POST['birthday'];
		$parent_name = $_POST['parent_name'];
		$parent_tel1 = $_POST['parent_tel1'];
		$parent_tel2 = $_POST['parent_tel2'];

		$updateStudent = array(
			'ID' => $ID,
			'name' => $name,
			'tel' => $tel,
			'sex' => $sex,
			'birthday' => $birthday,
			'parent_name' => $parent_name,
			'parent_tel1' => $parent_tel1,
			'parent_tel2' => $parent_tel2,
		);
		$Student_Model->updateStudent($updateStudent);
		seerDirect("./student_list.php?tel=".$tel."&active_update=1");
	}
}
// 学生添加
if ($action == 'add') {


	// 编辑画面初期化
	if(!$mode)
	{
		include View::getView('header');
		require_once View::getView('user/student_update');
		include View::getView('footer');
		View::output();
	}
	// 编辑信息提交
	else if($mode=="commit")
	{
		$name = $_POST['name'];
		$tel = $_POST['tel'];
		$sex = $_POST['sex'];
		$birthday = $_POST['birthday'];
		$parent_name = $_POST['parent_name'];
		$parent_tel1 = $_POST['parent_tel1'];
		$parent_tel2 = $_POST['parent_tel2'];

		$addStudent = array(
			'name' => $name,
			'tel' => $tel,
			'sex' => $sex,
			'birthday' => $birthday,
			'parent_name' => $parent_name,
			'parent_tel1' => $parent_tel1,
			'parent_tel2' => $parent_tel2,
		);
		$Student_Model->addStudent($addStudent);
		seerDirect("./student_list.php?tel=".$tel."&active_add=1");
	}
}
// 用户→学生初期化
if ($action == 'userToStudent' ) {

	// 画面初期化
	if(!$mode) {
		include View::getView('header');
		require_once View::getView('user/user_to_student');
		include View::getView('footer');
		View::output();
	}
	// 用户转学生信息提交
	else if($mode=="commit")
	{
		$wx_openid = $_POST['wx_openid'];
		if($Student_Model->userToStudent($wx_openid))
		{
			$addSuccess="succ";
		}
		else
		{
			$addErr="err";
		}
		include View::getView('header');
		require_once View::getView('user/user_to_student');
		include View::getView('footer');
		View::output();
	}

}